//
//  CustomButton.m
//  GLB
//
//  Created by fengjie on 2019/2/12.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "CustomButton.h"

@interface CustomButton ()
/**图片*/
@property (nonatomic,strong)UIImageView*imageView;
/**文字*/
@property (nonatomic,strong)UILabel*titleLabel;
/**当水平方向时 图片是否在👈，为NO就是在右边 */
@property (nonatomic,assign)BOOL isLeft;
@end

@implementation CustomButton

+(instancetype)buttonWithTitle:(NSString *)title Image:(NSString *)image{
    CustomButton * button = [[CustomButton alloc] initWithFrame:CGRectZero type:NO imageIsLeft:NO];
    button.imageView.image = IMAGE_NAMED(image);
    button.titleLabel.text = title;
    button.titleLabel.textColor =RGB(112, 112, 112);
    return button;
}
/**初始化文字图片在水平方向视图
 title:文字
 imag:图片名称
 isLeft:图片是否在👈，为NO就是在右边
 */
+(instancetype)buttonWithHorizontalTitle:(NSString *)title Image:(NSString *)image imageIsLeft:(BOOL)isLeft{
    CustomButton * button = [[CustomButton alloc] initWithFrame:CGRectZero type:YES imageIsLeft:isLeft];
    button.imageView.image = IMAGE_NAMED(image);
    button.titleLabel.text = title;
    button.titleLabel.textColor =RGB(112, 112, 112);
    return button;
}
-(instancetype)initWithFrame:(CGRect)frame type:(BOOL)type imageIsLeft:(BOOL)isLeft{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)]];
        self.isLeft = isLeft;
        [self addSubview:self.imageView];
        [self addSubview:self.titleLabel];
        if (type) {
            [self horizontal];
        }else{
            [self vertical];
        }
    }
    return self;
}

/**水平方向*/
-(void)horizontal{
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        if (self.isLeft) {
            make.left.mas_equalTo(5);
        }else{
            make.right.mas_equalTo(-5);
        }
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        if (self.isLeft) {
            make.right.mas_equalTo(-5);
            make.left.mas_equalTo(15);
        }else{
            make.left.mas_equalTo(5);
            make.right.mas_equalTo(-30);
        }
        
    }];

}
/**竖直方向*/
-(void)vertical{
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.imageView.mas_bottom).offset(5);
    }];

}
-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}
-(void)setTitleFont:(UIFont *)titleFont{
    _titleFont = titleFont;
    self.titleLabel.font = titleFont;
}
-(void)setTitleColor:(UIColor *)titleColor{
    _titleColor = titleColor;
    self.titleLabel.textColor = titleColor;

}
-(void)tapAction{
    BlockSafeRun(self.handler,self);
}
#pragma mark --懒加载

-(UIImageView*)imageView{
    if(!_imageView){
        _imageView=[UIImageView new];
        _imageView.userInteractionEnabled = YES;
        
    }
    return _imageView;
}
-(UILabel*)titleLabel{
    if(!_titleLabel){
        _titleLabel=[UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor =RGB(112, 112, 112);
        _titleLabel.font = SYSTEMFONT(12);
        _titleLabel.userInteractionEnabled = YES;
    }
    return _titleLabel;
}
@end


@implementation UnderlineButton
#pragma mark --创建一个带有下划线的按钮
+(instancetype)creUnderlineButton:(NSString *)title titleColor:(UIColor *)titleColor lineColor:(UIColor *)lineColr target:(id)target action:(SEL)action{
    
    UnderlineButton * button = [UnderlineButton buttonWithType:UIButtonTypeCustom];
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:title];
    
    [attributeString addAttribute:NSUnderlineStyleAttributeName
                            value:@(NSUnderlineStyleSingle)
                            range:(NSRange){0,[attributeString length]}];
    //此时如果设置字体颜色要这样
    [attributeString addAttribute:NSForegroundColorAttributeName value:titleColor  range:NSMakeRange(0,[attributeString length])];
    
    //设置下划线颜色...
    [attributeString addAttribute:NSUnderlineColorAttributeName value:lineColr range:(NSRange){0,[attributeString length]}];
    
    [button setAttributedTitle:attributeString forState:UIControlStateNormal];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

@end
